﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThouShaltNotBrick.Models;
using System.Xml.Linq;
using System.IO;
using GMLogging;

namespace ThouShaltNotBrick.DataAccess
{
    public static class BrickStatesXmlReader
    {
        #region Methods
        public static BrickStatesData Read(string pBrickStatesFilePath)
        {
            BrickStatesData brickStatesData = null;
            FileStream stream = null;
            try
            {
                stream = new FileStream(pBrickStatesFilePath, FileMode.Open, FileAccess.Read);
                XDocument brickStatesXDocument = XDocument.Load(stream);
                brickStatesData = Parse(brickStatesXDocument);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            return brickStatesData;
        }

        public static BrickStatesData Parse(string pBrickStatesInputString)
        {
            try
            {
                XDocument brickStatesXDocument = XDocument.Parse(pBrickStatesInputString);
                return Parse(brickStatesXDocument);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                throw;
            }
        }

        private static BrickStatesData Parse(XDocument pBrickStatesXDocument)
        {
            XElement brickStatesXElement = pBrickStatesXDocument.Element(BrickStatesXmlConstants.TAG_BRICK_STATES);

            List<XElement> brickStateXElements = brickStatesXElement.Elements(BrickStatesXmlConstants.TAG_BRICK_STATE).ToList();
            List<BrickStateData> brickStateList = new List<BrickStateData>();
            foreach (XElement brickStateXElement in brickStateXElements)
            {
                XAttribute nameXAttribute = brickStateXElement.Attribute(BrickStatesXmlConstants.TAG_BRICK_STATE_ATTRIBUTE_NAME);
                XAttribute isIndestructibleXAttribute = brickStateXElement.Attribute(BrickStatesXmlConstants.TAG_BRICK_STATE_ATTRIBUTE_IS_INDESTRUCTIBLE);
                XAttribute textureFileNameXAttribute = brickStateXElement.Attribute(BrickStatesXmlConstants.TAG_BRICK_STATE_ATTRIBUTE_TEXTURE_FILE_NAME);

                string name = nameXAttribute.Value;
                bool isIndestructible = bool.Parse(isIndestructibleXAttribute.Value);
                string textureFileName = textureFileNameXAttribute.Value;
                BrickStateData brickState = new BrickStateData(name, isIndestructible, textureFileName);
                brickStateList.Add(brickState);
            }

            BrickStatesData brickStates = new BrickStatesData(brickStateList);
            return brickStates;
        }
        #endregion
    }
}